11.2 Mosaic plots and treemaps
library(ggmosaic)
Figure 11.3
### Figure 11.3 - data
bridges <- read.csv("./data/bridges.csv")
bridges %>% sapply(class)
IDENTIF RIVER LOCATION ERECTED PURPOSE LENGTH LANES CLEAR.G T.OR.D MATERIAL SPAN REL.L
"character" "character" "numeric" "character" "character" "character" "integer" "character" "character" "character" "character" "character"
TYPE
"character"
bridges %>% head()
### 재료(MATERIAL) - factor()
bridges <- bridges %>%
mutate(MATERIAL = factor(MATERIAL, levels = c("WOOD", "STEEL", "IRON")))
### Figure 11.3 - 기본(원자료)
bridges %>%
filter(!is.na(MATERIAL)) %>%
ggplot() +
geom_mosaic(aes(x = product(MATERIAL, ERECTED), fill=MATERIAL),
offset = 0, color = "white")

### Figure 11.3 - 기본(원자료) + 텍스트
bridges %>%
filter(!is.na(MATERIAL)) %>%
ggplot() +
geom_mosaic(aes(x = product(MATERIAL, ERECTED), fill=MATERIAL),
offset = 0, color = "white") +
geom_mosaic_text(aes(x = product(MATERIAL, ERECTED), fill=MATERIAL))

### Figure 11.3 - 기본(원자료) + 숫자
bridges %>%
ggplot() +
geom_mosaic(aes(x = product(MATERIAL, ERECTED), fill=MATERIAL),
offset = 0, color = "white") +
geom_text(data = layer_data() %>% filter(.wt > 0),
aes(x = (xmin + xmax) / 2,
y = (ymin + ymax) / 2,
label = .wt), color="white")

### Figure 11.3 - 원자료
bridges %>%
filter(!is.na(MATERIAL)) %>%
ggplot() +
geom_mosaic(aes(x = product(MATERIAL, ERECTED), fill=MATERIAL),
offset = 0, color = "white") +
geom_text(data = layer_data() %>% filter(.wt > 0),
aes(x = (xmin + xmax) / 2,
y = (ymin + ymax) / 2,
label = .wt), color="white") +
scale_y_productlist(name = "", expand = c(0, 0), breaks = NULL) +
scale_x_productlist(name = "", expand = c(0, 0)) +
scale_fill_manual(name = "",
values = c("#D55E00D0", "#0072B2D0", "#009E73D0")) +
theme_minimal() +
theme(
panel.grid.major.x = element_blank(),
panel.grid.major.y = element_blank()
)

### 자료 요약
# (1) group_by - summarise
bridges_summ <- bridges %>%
filter(!is.na(MATERIAL)) %>%
group_by(MATERIAL, ERECTED) %>%
summarise(count=n())
`summarise()` has grouped output by 'MATERIAL'. You can override using the `.groups` argument.
# (2) table - melt
bridges_summ <- select(bridges, MATERIAL, ERECTED) %>%
table() %>%
reshape2::melt(value.name = "count")
### Figure 11.3 - 기본
bridges_summ %>%
ggplot() +
geom_mosaic(aes(weight=count,
x = product(MATERIAL, ERECTED), fill=MATERIAL),
offset = 0, color = "white")

### Figure 11.3
bridges_summ %>%
ggplot() +
geom_mosaic(aes(weight=count,
x = product(MATERIAL, ERECTED), fill=MATERIAL),
offset = 0, color = "white") +
geom_text(data = layer_data() %>% filter(.wt > 0),
aes(x = (xmin + xmax) / 2,
y = (ymin + ymax) / 2,
label = .wt), color="white") +
scale_y_productlist(name = "", expand = c(0, 0), breaks = NULL) +
scale_x_productlist(name = "", expand = c(0, 0)) +
scale_fill_manual(name = "",
values = c("#D55E00D0", "#0072B2D0", "#009E73D0")) +
theme_minimal() +
theme(
panel.grid.major.x = element_blank(),
panel.grid.major.y = element_blank()
)

Figure 11.5
### Figure 11.5 - data
US_census <- read.csv("./data/US_census.csv")
US_regions <- read.csv("./data/US_regions.csv")
population_df <- left_join(US_census, US_regions) %>%
group_by(region, division, state) %>%
summarize(
pop2000 = sum(pop2000, na.rm = TRUE),
pop2010 = sum(pop2010, na.rm = TRUE),
area = sum(area)
) %>%
ungroup() %>%
mutate(
state = factor(state, levels = state),
region = factor(region,
levels = c("West", "South", "Midwest", "Northeast"))
)
Joining with `by = join_by(state)`
`summarise()` has grouped output by 'region', 'division'. You can override using the `.groups` argument.
### manually add colors
# hues
hues <- c(50, 100, 250, 300) # brown, green, blue, purple
# minimum and maximum population density
minpop <- min(population_df$pop2010)
maxpop <- max(population_df$pop2010)
# turn pop density into color
population_df_color <- population_df %>%
mutate(index = as.numeric(factor(region))) %>%
group_by(index) %>%
mutate(
value = (pop2010-minpop)/(maxpop-minpop), # 0~1
fill = scales::gradient_n_pal(
colorspace::sequential_hcl(
6,
h = hues[index],
c = c(45, 20),
l = c(30, 80),
power = .5
)
)(1-value)
)
### Figure 11.5
population_df_color %>%
ggplot(aes(area = area, subgroup = region, fill = fill)) +
geom_treemap(color = "white", size = 0.5*.pt, alpha = NA) +
geom_treemap_subgroup_text(
colour = "white",
place = "center", alpha = 0.7,
grow = T
) +
geom_treemap_subgroup_border(color = "white") +
geom_treemap_text(
aes(label = state),
color = "black",
place = "center",
grow = F
) +
scale_fill_identity() +
guides(colour = "none", fill = "none")

### region colors
col_n <- population_df$region %>% table()
population_df_color <- population_df %>%
arrange(region, area %>% desc()) %>%
mutate(
value = (pop2010-minpop)/(maxpop-minpop),
fill = mapply(colorspace::lighten,
colorspace::qualitative_hcl(length(col_n), palette = "Dark 3"),
mapply(seq, -0.2, 0.8, length=col_n)) %>% unlist()
)
population_df_color %>%
ggplot(aes(area = area, subgroup = region, fill = fill)) +
geom_treemap(color = "white", size = 0.5*.pt, alpha = NA) +
geom_treemap_subgroup_text(
colour = "white",
place = "center", alpha = 0.7,
grow = T
) +
geom_treemap_subgroup_border(color = "white") +
geom_treemap_text(
aes(label = state),
color = "black",
place = "center",
grow = F
) +
scale_fill_identity() +
guides(colour = "none", fill = "none")
